
$erroractionpreference = "SilentlyContinue" 
#Create the GUI Box
Function Read-MultiLineInputBoxDialog()
{
    Add-Type -AssemblyName System.Drawing
    Add-Type -AssemblyName System.Windows.Forms

# Create the Label.
    $label = New-Object System.Windows.Forms.Label
    $label.Location = New-Object System.Drawing.Size(10,10) 
    $label.Size = New-Object System.Drawing.Size(280,20)
    $label.AutoSize = $true
    $label.Text = "For IBM user enable, please double check the user exists in BP 
and the BP manager has approved.

Please put ticket,ID,type(1.IBM,2.EA,3.WNS,4.Concentrix),
Reset password(Y/N) like showed below:"
	
    # Create the TextBox used to capture the user's text.
    $Request = New-Object System.Windows.Forms.TextBox 
    $Request.Location = New-Object System.Drawing.Size(10,80) 
	$Request.Size = New-Object System.Drawing.Size(300,150)
    $Request.AcceptsReturn = $true
    $Request.AcceptsTab = $false
    $Request.Multiline = $true
    $Request.ScrollBars = 'Both'
    $Request.Text = "SR123456,chaoyuew,1,n"

    # Create the OK button.
    $OKButton= New-Object System.Windows.Forms.Button
    $OKButton.Location = New-Object System.Drawing.Size(50,250)
    $OKButton.Size = New-Object System.Drawing.Size(50,25)
    $OKButton.Text = "OK"
    $OKButton.Add_Click({ $temp= $Request.Text; $form.Close() })

	# Create the ClearText button.
    $ClearText = New-Object System.Windows.Forms.Button
    $ClearText.Location = New-Object System.Drawing.Size(120,250)
    $ClearText.Size = New-Object System.Drawing.Size(50,25)
    $ClearText.Text = "Clear"
    $ClearText.Add_Click({ $Request.Text = $null})

		# Create the Quit button.
    $QuitButton = New-Object System.Windows.Forms.Button
    $QuitButton.Location = New-Object System.Drawing.Size(200,250)
    $QuitButton.Size = New-Object System.Drawing.Size(100,25)
    $QuitButton.Text = "Quit The Script"
    $QuitButton.Add_Click({ $temp="quit"; $form.Close() })
	
    # Create the form.
    $form = New-Object System.Windows.Forms.Form 
    $form.Text = "Enable User Management"
    $form.Size = New-Object System.Drawing.Size(350,320)
    $form.FormBorderStyle = 'FixedSingle'
    $form.StartPosition = "CenterScreen"
    $form.AutoSizeMode = 'GrowAndShrink'
    $form.Topmost = $True
  # $form.AcceptButton = $AddSendButton
  # $form.CancelButton = $AddReceiveButton
    $form.ShowInTaskbar = $true

    # Add all of the controls to the form.
    $form.Controls.Add($label)
	$form.Controls.Add($Request)
	$form.Controls.Add($ClearText)
    $form.Controls.Add($OKButton)
	$form.Controls.Add($QuitButton)
	
    # Initialize and show the form.
    $form.Add_Shown({$form.Activate()})
    $form.ShowDialog() > $null   # Trash the text of the button that was clicked.

    # Return the text that the user entered.
     return $temp
}
# Update the data that user enters
$res = Read-MultiLineInputBoxDialog
clear-content .\NoExitID.txt
if ($res -ne "quit")
{
$redetails = $res.split("`n")
$Pwd =  read-host "Please enter a new Password for user" -assecurestring
$Exd = (Get-Date).AddDays(365)
foreach ($detail  in $redetails)
{
	$Ticket=$detail.split(',')[0]
	$User=$detail.split(',')[1]
	$null = get-aduser $User
	if($? -eq $false)
		{
		echo $User >> .\NoExitID.txt
		continue
		}
	$UserType = $detail.split(',')[2]
#<------------------------------------1. Enable User
	$Notes ="`r`n" + $Ticket + " - Account Enabled - " +(get-date).ToString('dd/MM/yyyy')
	Unlock-ADAccount -Server $DC -Identity $User
	Set-Aduser -Server $DC -Identity $User -Enabled $True 
	$answer  =$detail.split(',')[3]
	if($answer -ieq "Y")#reset password
		{
			Set-ADAccountPassword -Reset -NewPassword $Pwd  -Server $DC -Identity $User
			Set-Aduser -Server $DC -Identity $User -AccountExpirationDate $Exd -ChangePasswordAtLogon $true
		}
		#add update log
	$Newinfo = (get-Aduser -Server $DC -Identity $User -Properties info).info +$Notes
	Set-Aduser -Server $DC -Identity $User -Replace @{'info'=$Newinfo }
	$erroractionpreference = "SilentlyContinue" 
#<------------------------------------2. Set-Up User Home Drive
	if ( (Test-Path "\\domain.internal\sharedfolders\users$\$ID") -eq $false)
		{
		new-item -path  \\domain.internal\sharedfolders\users$\ -name $User -itemtype directory
		}
		Add-NTFSAccess -Path \\domain.internal\sharedfolders\users$\$User `
		-Account EE\$User -AccessRights Modify 
#<------------------------------------3. Move User To Correct OU
	Switch($UserType )
	{
	1 {#update user
		Set-Aduser -Server $DC -Identity $User -ChangePasswordAtLogon $false -AccountExpirationDate $0
		Get-Aduser -Server $DC -Identity $User | Move-ADObject  –TargetPath `
		"OU=xx,OU=x,DC=domain,DC=internal"
			}
	2 {#update user
		Get-Aduser -Server $DC -Identity $User | Move-ADObject  –TargetPath `
		"OU=xxxx,OU=xxx,OU=xx,OU=x,DC=domain,DC=internal"
		}	
	3 {#update user
		Get-Aduser -Server $DC -Identity $User | Move-ADObject  –TargetPath `
		"OU=xxx,OU=xx,OU=x,DC=domain,DC=internal"
			}
	4 {#update user
		Get-Aduser -Server $DC -Identity $User | Move-ADObject  –TargetPath `
		"OU=xxx,OU=xx,OU=x,DC=domain,DC=internal"
		}
	}
#<------------------------------------4. Set-Up User Mailbox
$erroractionpreference = "SilentlyContinue" 
	$usertype = Get-Aduser -Server $DC -Identity $User|Select-String "OU=xx,OU=x,DC=domain,DC=internal"
	if(get-mailbox $User){Set-Mailbox -Identity EE\$User -HiddenFromAddressListsEnabled $false
continue}
	elseif($usertype -eq $null)
		{
		$n1 = Get-Random 1,2,3,4
		$n2 = Get-Random 5,6
		$MDB = "xx" + $n1 #随机选择DB
		$ARC = "xx" + $n2 #随机选择ArchiveDB
		Enable-Mailbox -Identity $User -Alias $User -Database $MDB  -ActiveSyncMailboxPolicy 'Default'
		Enable-Mailbox -Identity $User -Archive -ArchiveDatabase $ARC
		Set-CASMailbox -Identity $User -OWAEnabled $false
		}
}
}
if ((cat .\NoExitID.txt) -ne $null){ii .\NoExitID.txt}
